package com.youngjun.user.service.impl;

import com.youngjun.user.mapper.FunctionMapper;
import com.youngjun.user.pojo.domain.Function;
import com.youngjun.user.pojo.dto.FunctionDTO;
import com.youngjun.user.pojo.vo.FunctionVO;
import com.youngjun.user.pojo.vo.InterfaceVO;
import com.youngjun.user.service.AllowAccessFunctionRoleService;
import com.youngjun.user.service.AllowAssignFunctionRoleService;
import com.youngjun.user.service.FunctionInterfaceService;
import com.youngjun.user.service.FunctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class FunctionServiceImpl implements FunctionService {

    @Autowired
    FunctionMapper functionMapper;

    @Autowired
    FunctionInterfaceService functionInterfaceService;

    @Autowired
    AllowAccessFunctionRoleService allowAccessFunctionRoleService;

    @Autowired
    AllowAssignFunctionRoleService allowAssignFunctionRoleService;

    @Override
    public FunctionMapper getMapper() {
        return functionMapper;
    }

    @Override
    public List<FunctionVO> queryAllowAccessFunctionList(Long roleId) {
        return selectAllVO(FunctionVO.class);
    }

    @Override
    public List<FunctionVO> queryAllowAssignFunctionList(Long roleId) {
        return selectAllVO(FunctionVO.class);
    }

    @Override
    public List<FunctionVO> queryList() {
        return selectAllVO(FunctionVO.class);
    }

    @Override
    public FunctionVO create(FunctionDTO dto) {
        Function function = new Function().mapFrom(dto);
        insert(new Function().mapFrom(dto));
        return new FunctionVO().mapFrom(function);
    }

    @Override
    public FunctionVO modify(FunctionDTO dto) {
        Function function = new Function().mapFrom(dto);
        updateByPrimaryKey(new Function().mapFrom(dto));
        return new FunctionVO().mapFrom(function);
    }

    @Override
    public void delete(Long id) {
        deleteWithLogic(id);
        functionInterfaceService.deleteWithLogicByFunctionId(id);
        allowAccessFunctionRoleService.deleteWithLogicByFunctionId(id);
        allowAssignFunctionRoleService.deleteWithLogicByFunctionId(id);
    }

}